package com.lw.sbdemo2.common.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * <p>标题: swagger启动配置</p>
 * <p>描述: 通过@Configuration注解，让Spring来加载该类配置；通过@EnableSwagger2注解来启用Swagger2</p>
 * <p>描述: http://localhost:8801//swagger-ui.html</p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2018/1/23
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    /**
     * 构建一个DocketBean
     * @return docket
     */
    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select()
                //设置查找的路径，如果某个接口不想暴露,可以使用@ApiIgnore注解
                //.apis(RequestHandlerSelectors.basePackage("com.zoe.demo.web"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建api文档的详细信息函数
     * @return apiInfo
     */
    private ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                //设置文档的标题
                .title("SpringBoot2")
                //设置文档的描述
                .description("SpringBoot2模板服务")
                //设置文档的License信息
                .termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
                .build();
    }
}
